<?php
/**
* for create selector from specify ctrl name
* the parameter sent pass url
* 
* @param ctrl the controller name
* @param filter the where condition [optional]
* @param name the HTML selector name [optional]
* @return string html selector
* @example
* <code>
*    var div_subcate = document.getElementById('subcategory_container');
*    var category = document.getElementById('form_category_id_');
*    var filter = "&filter=scy_category_id = '" + category.value + "'";
*    var name = "&name=form[subcategory_id]";
*    var url = "index.php?module=InnoForum&type=jquery&func=selector&ctrl=Sub_Category" + filter + name + genSession() ;
*    //alert(url);
*    new Ajax.Request(url, {
*        method:'get',
*        onSuccess: function(transport){
*         var result = transport.responseText;
*         div_subcate.innerHTML= result;
*         }	   
*      });
*    
*  }
* </code>
*/

/**
* for get jquery data for specify ctrl name
* the template name format "jquery_$func_$ctrl.htm"
*/
function InnoForum_jquery_list() {
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'list' , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');




    $pagesize = FormUtil::getPassedValue ('pagesize', 100 , 'GET');
    $render = pnRender::getInstance('InnoForum');
    $render->assign ('_GET', $_GET);
    $render->assign ('_POST', $_POST);
    $render->assign ('_REQUEST', $_REQUEST);

    if ($class = Loader::loadClassFromModule ('InnoForum', 'Ajax' . ucfirst($ctrl), true)){

      $objectArray = new $class ();

      $where   = null;
      $sort = null;
      if (method_exists($objectArray,'genPermission')){
        $permit = $objectArray->genPermission();
        if (empty($permit)){
          return LogUtil::registerError("ส่วนนี้ถูกจำกัดให้ใช้ได้เฉพาะสมาชิกเท่านั้น");
        }
      }
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }

      if (method_exists($objectArray,'genSort')){
        $sort = $objectArray->genSort();
      }
      if (method_exists($objectArray,'selectExtendResult')){
        $resultex = $objectArray->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      if (empty($where)){
        $where = $objectArray->_objWhere;
      }
      if (empty($sort)){
        $sort = $objectArray->_objSort;
      }
      $objectArray->get ($where, $sort , $offset, $pagesize);
      //var_dump($objectArray);
      //assign to view

    //print_r($objectArray->_objData); exit;

      $render->assign('objectArray', $objectArray->_objData);
    }
    $render->assign('ctrl', $ctrl);
    $render->assign('setting',InnoUtil::getSetting());
    if ($lang){
      $render->assign('lang', $lang);
    }else{
      $render->assign('lang', pnUserGetLang());
    }
    echo $render->fetch('jquery_'.$func.'_'.strtolower($ctrl).'.htm');
    return true;
}

function InnoForum_jquery_view(){
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'view' , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    $where   = '';

    $render = pnRender::getInstance('InnoForum');
    $render->assign ('_GET', $_GET);
    $render->assign ('_POST', $_POST);
    $render->assign ('_REQUEST', $_REQUEST);
    
    if ($id){
      //load class
      if (!($class = Loader::loadClassFromModule ('InnoForum', 'Ajax' . ucfirst($ctrl), false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();

      if (method_exists($object,'genPermission')){
        $permit = $object->genPermission();
        if (empty($permit)){
          return LogUtil::registerError ("ส่วนนี้ถูกจำกัดให้ใช้ได้เฉพาะสมาชิกเท่านั้น");
        }
      }

      $object->get($id);
      if (method_exists($object,'selectExtendResult')){
        $resultex = $object->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      $render->assign ('view', $object->_objData);
    }
    $render->assign('ctrl', $ctrl);
    $render->assign('setting',InnoUtil::getSetting());
    if ($lang){
      $render->assign('lang', $lang);
    }else{
      $render->assign('lang', pnUserGetLang());
    }
    echo $render->fetch('jquery_'.$func.'_'.strtolower($ctrl).'.htm');
    return true;
}


  function fixEncoding($in_str) { 
    $cur_encoding = mb_detect_encoding($in_str) ; 
    /*
      $mb_encoding  = mb_check_encoding($in_str,"UTF-8");
      return $cur_encoding."<br>mb :".$mb_encoding;
    */
    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) 
      return $in_str; 
    else 
      return utf8_encode($in_str); 

  } // fixEncoding 